package com.dsbj.mp.icardservice.service.asynctask;

import com.dsbj.mp.icardservice.entity.ReservationPrimary;
import com.dsbj.mp.icardservice.service.ReservationService;
import com.dsbj.mp.icardservice.utils.Constants;
import com.dsbj.mp.icardservice.utils.beanfactory.SpringBeanFactoryUtil;
import com.dsbj.mp.icardservice.webserviceclient.dsbjpmc.AllowUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;

/**
 * 异步任务,将访客预约单提交至OA端。
 *
 * @param
 * @author cjw 2019-12-19
 * @return
 * @deprecated 待删 未使用该服务类。
 */
@Deprecated
public class AsyncTask_submitReservationPrimary_toOA {
	
	/**
	 * 类AsyncTask_submitReservationPrimary_toOA.Info。
	 *
	 * @author cjw 2018-11-08
	 */
	public static class Info {
		private static ReservationService reservationService = SpringBeanFactoryUtil.getBean(ReservationService.class);
		private AllowUser allowUser;
		private ReservationPrimary rpy_oa;
		private ReservationPrimary reservationPrimary;
		
		/**
		 * 构造函数。
		 *
		 * @param
		 * @return
		 * @author cjw 2019-12-19
		 */
		public Info(AllowUser allowUser, ReservationPrimary rpy_oa, ReservationPrimary reservationPrimary) {
			this.allowUser = allowUser;
			this.rpy_oa = rpy_oa;
			this.reservationPrimary = reservationPrimary;
		}
		
		/**
		 * 处理info信息，将访客预约单提交至OA端。
		 *
		 * @param
		 * @return
		 * @author cjw 2019-12-19
		 */
		public String processInfo() {
			try {
				String result = null;
				result = reservationService.submitReservationPrimary_toOA(allowUser, rpy_oa, reservationPrimary);
				return result;
			} catch (Exception e) {
				logger.error(new StringBuffer().append("异步任务: 将访客预约单提交至OA端,失败!").append(e.toString()).append("\n\n").toString());
			}
			return Constants.ERROR;
		}
	}
	
	private static Logger logger = LoggerFactory.getLogger(AsyncTask_submitReservationPrimary_toOA.class);
	private LinkedBlockingQueue<Info> msgQueue;
	private ExecutorService executorService;
	private volatile static AsyncTask_submitReservationPrimary_toOA INSTANCE;
	
	/**
	 * 构造函数
	 */
	private AsyncTask_submitReservationPrimary_toOA() {
		logger.info("执行了AsyncTask_submitReservationPrimary_toOA()构造方法..");
		
		msgQueue = new LinkedBlockingQueue<>(10000);
		// 添加处理线程
		int workerNum = 1;
		executorService = Executors.newFixedThreadPool(workerNum, new ThreadFactory() {
			public Thread newThread(Runnable r) {
				Thread thread = Executors.defaultThreadFactory().newThread(r);
				thread.setDaemon(true);
				return thread;
			}
		});
		
		for (int i = 0; i < workerNum; ++i) {
			executorService.execute(new Runnable() {
				public void run() {
					while (true) {
						// 先用 take 获取数据
						try {
							Info info = msgQueue.take();
							info.processInfo();
						} catch (InterruptedException e) {
							logger.warn("处理异步任务时,发生错误!"
									+ "\n读取异步任务时,出现中断异常InterruptedException," + e.toString());
						}
					}
				}
			});
		}
	}
	
	/**
	 * 单例，双重校验，请在 JDK 1.5及更高版本中使用。
	 *
	 * @return
	 */
	public static AsyncTask_submitReservationPrimary_toOA getInstance() {
		if (INSTANCE == null) {
			synchronized (AsyncTask_submitReservationPrimary_toOA.class) {
				if (INSTANCE == null) {
					INSTANCE = new AsyncTask_submitReservationPrimary_toOA();
				}
			}
		}
		return INSTANCE;
	}
	
	/**
	 * 提交异步任务。
	 *
	 * @param allowUser
	 * @param rpy_oa
	 * @param reservationPrimary
	 */
	public void submitAsyncTask(AllowUser allowUser, ReservationPrimary rpy_oa, ReservationPrimary reservationPrimary) {
		
		Info info = new Info(allowUser, rpy_oa, reservationPrimary);
		boolean isSubmited = msgQueue.offer(info);
		if (isSubmited == false) {
			logger.error("提交异步任务失败,队列已满!");
		}
		logger.info("执行了submitAsyncTask(),msgQueue.size()=" + msgQueue.size());
	}
	
}
